package com.android.pos;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.pos.db.POSDbAdapter;

public class FoodSelection extends Activity {
	
	  private POSDbAdapter mDbHelperItem;  
	  
	  private static final String Flag = "FoodSelection";
	  private Button BtnConfirmOrder;
	  	  
	  ArrayList<MyItem> arItem;	  
	  
	  public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        // food_selection.xml로 화면을 그린다.	
	        setContentView(R.layout.food_selection);
	        
	        
	        // when click new order button
	        BtnConfirmOrder = (Button)findViewById(R.id.btn_chooseitem);
	        BtnConfirmOrder.setOnClickListener(new Button.OnClickListener() {

				public void onClick(View arg0) {
		        	Context context = getApplicationContext();        	
					Log.d(Flag,"Confirm! Go back to summary menu!");
					
					// Prepare to return intent...
					// Call the
					FoodSelectionDataManipulation dataMan = new FoodSelectionDataManipulation();
					Log.d(Flag,"20001");
					Intent intent = new Intent();
					Log.d(Flag,"20002");
					
//					intent.putStringArrayListExtra("FoodArray", dataMan.setFoodSelectionDataArray());
					Log.d(Flag,"20003");
					//intent.putExtra("FoodArray", );
					
				}

	        });
	        
	        
	        arItem = new ArrayList<MyItem>();
	        MyItem mi;
	        
	    	// initialization for DB connection
			mDbHelperItem = new POSDbAdapter(this);
			mDbHelperItem.open();
			
			// 모든 아이템(메뉴)를 갖고와서 커서에 저장
			Cursor itemCursor = mDbHelperItem.fetchAllItems();
			// 안드로이드 life cycle 관리를 위한 커서 매니징
			startManagingCursor(itemCursor);
			
			String vStr1;
			String vStr2;
			
			// 배열에 담는다.
			while(itemCursor.moveToNext()){
				vStr1 = itemCursor.getString(1);
				vStr2 = itemCursor.getString(2);
				
				mi = new MyItem(vStr1, vStr2);
				arItem.add(mi);
				
			}	
	        
//	        mi = new MyItem("pizza", "500"); arItem.add(mi);
//	        mi = new MyItem("cola", "150"); arItem.add(mi);
//	        mi = new MyItem("cider", "150"); arItem.add(mi);
	        
	        MyListAdapter MyAdapter = new MyListAdapter(
	        		this, R.layout.checkbox_view, arItem);
	        
	        ListView MyList;
	        MyList = (ListView)findViewById(R.id.list);
	        MyList.setAdapter(MyAdapter);
	  }
}
	        
// 리스트 뷰에 출력할 항목
class MyItem {
	  MyItem(String vFoodName, String vPrice){
		  FoodName = vFoodName;
		  Price = vPrice;
	  }
	  String FoodName;
	  String Price;
}

// 어댑터 클래스
class MyListAdapter extends BaseAdapter {
	Context maincon;
	LayoutInflater InfLater;
	ArrayList<MyItem> arSrc;
	int layout;
	
	public MyListAdapter(Context context, int alayout, ArrayList<MyItem> aarSrc){
		maincon = context;
		InfLater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		arSrc = aarSrc;
		layout = alayout;
	}
	
	public int getCount(){
		return arSrc.size();
	}
	
	public String getItem(int position){
		return arSrc.get(position).FoodName;
	}
	
	public long getItemId(int position){
		return position;
	}
	
	// 각 항목의 뷰 생성
	public View getView(int position, View convertView, ViewGroup parent){
		final int pos = position;
		if (convertView == null){
			convertView = InfLater.inflate(layout,parent,false);
			
		}
		
		CheckBox chk_box = (CheckBox)convertView.findViewById(R.id.check);
		
		TextView txt = (TextView)convertView.findViewById(R.id.item_name);
		txt.setText(arSrc.get(position).FoodName);
		
		TextView txt2 = (TextView)convertView.findViewById(R.id.price);
		txt2.setText(arSrc.get(position).Price);
		
		Spinner qty = (Spinner)convertView.findViewById(R.id.qty_spinner);

		return convertView;
	}
}


// 생성된 체크박스/텍스트/스피너 값을 배열에 담는 클래스
class FoodSelectionDataManipulation extends Activity {

		private static final String Flag = "FoodSelectionDataManipulation";	
		
		public ArrayList<String> setFoodSelectionDataArray(){
			Log.d(Flag,"200004");
			/*
				Define Food Array
					1. Food Name
					2. Price
					3. Qty
			*/
			
			// 이부분에서 에러...
//			TextView txt1 = (TextView)findViewById(R.id.item_name);
//			CheckBox chk_box = (CheckBox)findViewById(R.id.item_name);
//			
//			
//			Log.d(Flag,"chk_box.length..." + chk_box.length());
//			Log.d(Flag,"txt1.toString..." + txt1.toString());
			
			return null;
			
		}
}
